﻿using System.Data;
using System.Linq;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private readonly EFDbContext context = new EFDbContext();

        #region IProductRepository Members

        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }

        public void SaveProduct(Product product)
        {
            //if new product add it
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                //Product productToUpdate = context.Products.First(p => p.ProductID == product.ProductID);
                //productToUpdate.Category = product.Category;
                //productToUpdate.Description = product.Description;
                //productToUpdate.Name = product.Name;
                //productToUpdate.Price = product.Price;
                context.Products.Attach(product);
                context.Entry(product).State = EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void Delete(Product product)
        {
            context.Products.Remove(product);
            context.SaveChanges();
        }

        #endregion
    }
}